<template>
	<view>
		<view class="item" v-for="(address, index) in addressList" :key="index" @click="chooseAddress(index)">
			<view class="top">
				<view class="name">{{ address.name }}</view>
				<view class="phone">{{ address.phone }}</view>
				<!-- 	<view class="tag">
					<text v-for="(item, index) in res.tag" :key="index"
						:class="{red:item.tagText=='默认'}">{{ item.tagText }}</text>
				</view> -->
			</view>
			<view class="bottom">
				{{address.site+address.detailAddress}}
				<u-icon name="edit-pen" :size="40" color="#999999" @click='toUpdateAddress(index,address.address_Id)'>
				</u-icon>
			</view>
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				chooseSite: {},
				tradeNo: ""
			};
		},
		onLoad(option) {
			this.getAddressList()
			this.tradeNo = option.tradeNo
		},
		methods: {
			//获取地址列表数据
			getAddressList() {
				const username = uni.getStorageSync('username');
				uniCloud.callFunction({
					name: 'get-site',
					data: {
						username: username
					}
				}).then(res => {
					this.addressList = res.result
				})
			},
			//选择地址
			chooseAddress(i) {
				if (this.tradeNo) {
					this.chooseSite = this.addressList[i]
					this.$u.toast('选择成功')
					setTimeout(() => {
						this.$u.route({
							url: 'pages/createOrder/createOrder',
							params: {
								tradeNo: this.tradeNo,
								name: this.chooseSite.name,
								phone: this.chooseSite.phone,
								site: this.chooseSite.site,
								detailAddress: this.chooseSite.detailAddress
							}
						})
					}, 500)
				}


			},
			toAddSite() {
				uni.navigateTo({
					url: '/pages/address/addSite'
				});
			},
			toUpdateAddress(i, id) {
				this.chooseSite = this.addressList[i]
				this.$u.route('/pages/address/updateAddress', {
					index: i,
					name: this.chooseSite.name,
					phone: this.chooseSite.phone,
					site: this.chooseSite.site,
					detailAddress: this.chooseSite.detailAddress,
					address_Id: id
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.item {
		padding: 40rpx 20rpx;

		.top {
			display: flex;
			font-weight: bold;
			font-size: 34rpx;

			.phone {
				margin-left: 60rpx;
			}

			.tag {
				display: flex;
				font-weight: normal;
				align-items: center;

				text {
					display: block;
					width: 60rpx;
					height: 34rpx;
					line-height: 34rpx;
					color: #ffffff;
					font-size: 20rpx;
					border-radius: 6rpx;
					text-align: center;
					margin-left: 30rpx;
					background-color: rgb(49, 145, 253);
				}

				.red {
					background-color: red
				}
			}
		}

		.bottom {
			display: flex;
			margin-top: 20rpx;
			font-size: 28rpx;
			justify-content: space-between;
			color: #999999;
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 100rpx;
		position: absolute;
		bottom: 30rpx;
		left: 80rpx;
		background-color: #5098ff;
		border-radius: 60rpx;
		font-size: 30rpx;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>
